package com.aboo.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.aboo.dao.RRoleMenuDao;
import com.aboo.dao.RoleDao;
import com.aboo.po.MenuPO;
import com.aboo.po.Page;
import com.aboo.po.RRoleMenuPO;
import com.aboo.po.RolePO;
import com.aboo.pub.exception.BasicException;
import com.aboo.pub.result.ResultConstant;
import com.aboo.pub.result.ResultInfo;
import com.aboo.service.RoleService;

public class RoleServiceImpl implements RoleService {
	
	private RoleDao roleDao;
	private RRoleMenuDao rRoleMenuDao;

	public void saveRole(ResultInfo ri, RolePO param) throws BasicException {
		try {
			roleDao.save(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("保存角色信息时出错！", ex);
		}
	}

	public void deleteRole(ResultInfo ri, List<String> roleIds)	throws BasicException {
		try {
			if (roleIds != null) {
				String roleId = null;
				for(Iterator<String> iter = roleIds.iterator(); iter.hasNext();) {
					roleId = iter.next();
					if ("1".equals(roleId)) {
						throw new BasicException("超级管理员角色不允许删除！");
					}
					roleDao.delete(new Integer(roleId));
				}
			}
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("删除角色信息时出错！", ex);
		}
	}

	public void updateRole(ResultInfo ri, RolePO param) throws BasicException {
		try {
			roleDao.update(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("修改角色信息时出错！", ex);
		}
	}
	
	public void findRole(ResultInfo ri, RolePO param) throws BasicException {
		try {
			RolePO result = roleDao.find(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询角色信息时出错！", ex);
		}
	}

	public void findRoleList(ResultInfo ri, RolePO param, Page page) throws BasicException {
		try {
			if (page != null) {
				Integer count = roleDao.findCount(param);
				page.setTotalRecordSize(count);
			}
			
			List<RolePO> result = roleDao.findList(param, page);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询角色列表信息时出错！", ex);
		}
	}
	
	public void findRoleCount(ResultInfo ri, RolePO param) throws BasicException {
		try {
			Integer result = roleDao.findCount(param);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询角色数量时出错！", ex);
		}
	}

	public void findRoleMenuList(ResultInfo ri, Integer roleId)
			throws BasicException {
		try {
			List<MenuPO> result = roleDao.findMenuById(roleId);
			ri.setResultCode(ResultConstant.STATUS_OK);
			ri.setObj(result);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("查询角色对应的菜单信息时出错！", ex);
		}
	}

	public void deleteRoleMenuList(ResultInfo ri, Integer roleId,
			List<Integer> menuIds) throws BasicException {
		try {
			if (menuIds != null) {
				Integer menuId = null;
				for (Iterator<Integer> iter = menuIds.iterator(); iter.hasNext();) {
					menuId = iter.next();
					RRoleMenuPO po = new RRoleMenuPO();
					po.setRoleId(roleId);
					po.setMenuId(menuId);
					rRoleMenuDao.delete(po);
				}
			}
			
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("删除角色中的菜单信息时出错！", ex);
		}
	}

	public void saveRoleMenuList(ResultInfo ri, Integer roleId,
			List<String> menuIds) throws BasicException {
		try {
			List<String> oldMenuIds = new ArrayList<String>();
			List<MenuPO> menuResult = roleDao.findMenuById(roleId);
			MenuPO menu = null;
			if (menuResult != null) {
				for(Iterator<MenuPO> iter = menuResult.iterator(); iter.hasNext();) {
					menu = iter.next();
					oldMenuIds.add(menu.getId().toString());
				}
			}
			
			String menuId = null;
			//保存新增的关系
			if (menuIds != null) {
				for (Iterator<String> iter = menuIds.iterator(); iter.hasNext();) {
					menuId = iter.next();
					if (!oldMenuIds.contains(menuId)) {
						RRoleMenuPO po = new RRoleMenuPO();
						po.setRoleId(roleId);
						po.setMenuId(new Integer(menuId));
						rRoleMenuDao.save(po);
					}
				}
			}
			
			if (oldMenuIds != null) {
				for (Iterator<String> iter = oldMenuIds.iterator(); iter.hasNext();) {
					menuId = iter.next();
					if (!menuIds.contains(menuId)) {
						RRoleMenuPO po = new RRoleMenuPO();
						po.setRoleId(roleId);
						po.setMenuId(new Integer(menuId));
						rRoleMenuDao.delete(po);
					}
				}
			}
			
			ri.setResultCode(ResultConstant.STATUS_OK);
		} catch (BasicException ex) {
			throw ex;
		} catch (Exception ex) {
			throw new BasicException("保存角色中的菜单信息时出错！", ex);
		}
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	public RRoleMenuDao getrRoleMenuDao() {
		return rRoleMenuDao;
	}
	public void setrRoleMenuDao(RRoleMenuDao rRoleMenuDao) {
		this.rRoleMenuDao = rRoleMenuDao;
	}

}
